package io.renren.modules.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.car.dao.CarBaseDao;
import io.renren.modules.car.entity.CarBaseEntity;
import io.renren.modules.car.entity.CarSellMsgEntity;
import io.renren.modules.car.entity.CarShopMoneyEntity;
import io.renren.modules.car.entity.ConstantEnum;
import io.renren.modules.car.service.CarBaseService;
import io.renren.modules.car.service.CarSellMsgService;
import io.renren.modules.car.service.CarShopMoneyService;
import io.renren.modules.sys.entity.SysDictEntity;
import io.renren.modules.sys.service.SysDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Service("carBaseService")
public class CarBaseServiceImpl extends ServiceImpl<CarBaseDao, CarBaseEntity> implements CarBaseService {

    @Resource
    private CarShopMoneyService carShopMoneyService;
    @Resource
    private CarSellMsgService carSellMsgService;
    @Resource
    private SysDictService sysDictService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarBaseEntity> page = this.page(
                new Query<CarBaseEntity>().getPage(params),
                new QueryWrapper<CarBaseEntity>().eq("create_user", params.get("create_user")).orderByDesc("create_time")
        );

        List<CarSellMsgEntity> carSellMsgEntities = carSellMsgService.list(new QueryWrapper<CarSellMsgEntity>().eq("create_user",params.get("create_user")));

        List<CarShopMoneyEntity> carShopMoneyEntities = carShopMoneyService.list(new QueryWrapper<CarShopMoneyEntity>().eq("create_user", params.get("create_user")));

        List<SysDictEntity> colorList = sysDictService.list(new QueryWrapper<SysDictEntity>().eq("type", "car_color"));
        List<SysDictEntity> pfbzList = sysDictService.list(new QueryWrapper<SysDictEntity>().eq("type", "pfbz"));
        List<SysDictEntity> cxList = sysDictService.list(new QueryWrapper<SysDictEntity>().eq("type", "cx"));

        page.getRecords().stream().map(m -> {
            carSellMsgEntities.stream().filter(c->Objects.equals(m.getId(),c.getCarId())).forEach(c->{
                m.setBuyPrice(c.getBuyPrice());
                m.setOtherPrice(c.getOtherPrice());
                m.setLastMoney(c.getLastMoney());
                m.setState(c.getState());
            });

            carShopMoneyEntities.stream().filter(c->Objects.equals(m.getId(),c.getCarId())).forEach(c->{
                if (ObjectUtils.isEmpty(m.getCarShopMoneyEntities())){
                    m.setCarShopMoneyEntities(new ArrayList<>());
                }
                m.getCarShopMoneyEntities().add(c);
            });

            colorList.stream().filter(c->( c.getId().intValue() == Integer.valueOf(m.getColour()) )).forEach(c->{
                m.setColourZh(c.getValue());
            });
            pfbzList.stream().filter(c->( c.getId().intValue() == Integer.valueOf(m.getEmissionStandard()) )).forEach(c->{
                m.setEmissionStandardZh(c.getValue());
            });
            cxList.stream().filter(c->( c.getId().intValue() == Integer.valueOf(m.getModel()) )).forEach(c->{
                m.setModelZh(c.getValue());
            });

            return true;
        }).collect(Collectors.toList());


        List<CarBaseEntity> collect = page.getRecords().stream().filter(m -> Objects.equals(10, m.getState())).collect(Collectors.toList());
        page.setRecords(collect);


        return new PageUtils(page);
    }

    @Override
    public CarBaseEntity getMsgById(Integer id) {
        CarBaseEntity carBase = getById(id);
        Map<String,Object> param = new HashMap<>();


        param.put("carId",carBase.getId());
        List<Integer> list = carShopMoneyService.queryList(param);
        carBase.setFriendShopId(list);

        CarSellMsgEntity carSellMsgEntity = carSellMsgService.getOne(new QueryWrapper<CarSellMsgEntity>().eq("car_id", carBase.getId()));
        if (!ObjectUtils.isEmpty(carSellMsgEntity)){
            carBase.setBuyPrice(carSellMsgEntity.getBuyPrice());
            carBase.setOtherPrice(carSellMsgEntity.getOtherPrice());
            carBase.setLastMoney(carSellMsgEntity.getLastMoney());
            carBase.setSellPrice(carSellMsgEntity.getSellPrice());
            carBase.setSellRemark(carSellMsgEntity.getRemark());
            carBase.setState(carSellMsgEntity.getState());
        }

        List<CarShopMoneyEntity> carShopMoneyEntities = carShopMoneyService.list2(carBase);
//        List<CarShopMoneyEntity> carShopMoneyEntities = carShopMoneyService.list(new QueryWrapper<CarShopMoneyEntity>().eq("create_user", carBase.getCreateUser()).eq("car_id",carBase.getId()));
        carShopMoneyEntities.stream().forEach(c->{
            if (ObjectUtils.isEmpty(carBase.getCarShopMoneyEntities())){
                carBase.setCarShopMoneyEntities(new ArrayList<>());
            }
            carBase.getCarShopMoneyEntities().add(c);
        });

        return carBase;
    }

    @Override
    @Transactional
    public boolean saveWithFriendShop(CarBaseEntity carBase) {
        save(carBase);
        saveShopMoneyObj(carBase);
        saveCarSellMsg(carBase);
        return true;
    }

    private boolean saveCarSellMsg(CarBaseEntity carBase) {
        CarSellMsgEntity carSellMsgEntity2 = carSellMsgService.getOne(new QueryWrapper<CarSellMsgEntity>().eq("car_id", carBase.getId()));
        Date date = new Date();
        if (!ObjectUtils.isEmpty(carSellMsgEntity2)){
            carSellMsgEntity2.setBuyPrice(carBase.getBuyPrice());
            carSellMsgEntity2.setUpdateTime(date);
            carSellMsgService.updateById(carSellMsgEntity2);
        }else{
            CarSellMsgEntity carSellMsgEntity = new CarSellMsgEntity();
            carSellMsgEntity.setCarId(carBase.getId());
            carSellMsgEntity.setCreateUser(carBase.getCreateUser());
            carSellMsgEntity.setState(ConstantEnum.CarState.SELLING.getValue());
            carSellMsgEntity.setBuyPrice(carBase.getBuyPrice());
            carSellMsgEntity.setSellPrice(new BigDecimal(0));
            carSellMsgEntity.setOtherPrice(new BigDecimal(0));
            carSellMsgEntity.setCreateTime(date);
            carSellMsgEntity.setUpdateTime(date);
            carSellMsgService.save(carSellMsgEntity);
        }
        return true;
    }

    @Override
    public boolean updateByIdWithFriendShop(CarBaseEntity carBase) {
        boolean b = updateById(carBase);
        saveShopMoneyObj(carBase);
        saveCarSellMsg(carBase);
        return b;
    }

    @Transactional
    public boolean saveShopMoneyObj(CarBaseEntity carBase){
        Map<String,Object> removeParam = new HashMap<>();
        removeParam.put("shop_id",carBase.getShopId());
        removeParam.put("car_id",carBase.getId());
        carShopMoneyService.removeByMap(removeParam);
        if (!ObjectUtils.isEmpty(carBase.getFriendShopId())){
            for (Integer s : carBase.getFriendShopId()) {
                CarShopMoneyEntity carShopMoneyEntity = new CarShopMoneyEntity();
                carShopMoneyEntity.setShopId(carBase.getShopId());
                carShopMoneyEntity.setGetMoney(new BigDecimal(0));
                carShopMoneyEntity.setFriendShopId(s);
                carShopMoneyEntity.setCarId(carBase.getId());
                carShopMoneyEntity.setCreateUser(carBase.getCreateUser());
                Date date = new Date();
                carShopMoneyEntity.setCreateTime(date);
                carShopMoneyEntity.setUpdateTime(date);
                carShopMoneyService.save(carShopMoneyEntity);
            }
        }
        return true;
    }
}
